package com.example.demo.damai;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/**
 * @Author TCJ
 * @create 2023/10/30
 */
public class DaMaiService {
    private static final String LOGIN_URL = "https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F";
    private static final String HOME_URL = "https://www.damai.cn/";
    static {
        System.setProperty("webdriver.chrome.driver","src/main/resources/chromedriver.exe");
    }


    public void login(String account,String password){
        WebDriver webDriver = getWebDriver();
        webDriver.get(LOGIN_URL);
        webDriver.switchTo().frame("alibaba-login-box");
        WebElement accountInput = webDriver.findElement(By.id("fm-login-id"));
        WebElement passwordInput = webDriver.findElement(By.id("fm-login-password"));
        WebElement login = webDriver.findElement(By.tagName("button"));
        accountInput.sendKeys(account);
        passwordInput.sendKeys(password);
        login.click();
        webDriver.switchTo().defaultContent();
        sleep();
        WebElement element = webDriver.findElement(By.className("input-search"));
        element.sendKeys("凤凰传奇");
        webDriver.findElement(By.className("btn-search")).click();
        sleep();
        System.out.println(webDriver);
    }


    private void sleep(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private void sleep(Long millis){
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private WebDriver getWebDriver(){
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
        return new ChromeDriver(chromeOptions);
    }
    public static void main(String[] args) throws InterruptedException {
        DaMaiService daMaiService = new DaMaiService();
        daMaiService.login("17313408532","tcj990124");

    }

}
